package com.knowledgepoint.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledgepoint.study.mapper.KnowledgePointArticleMapper;
import com.knowledgepoint.study.model.entity.KnowledgePointArticle;
import com.knowledgepoint.study.service.KnowledgePointArticleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 知识点文章 Service 实现
 */
@Service
public class KnowledgePointArticleServiceImpl extends ServiceImpl<KnowledgePointArticleMapper, KnowledgePointArticle> implements KnowledgePointArticleService {

    @Override
    public List<KnowledgePointArticle> listByCategoryId(Integer categoryOneId, Integer categoryTwoId) {
        LambdaQueryWrapper<KnowledgePointArticle> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryOneId != null) {
            queryWrapper.eq(KnowledgePointArticle::getCategoryOneId, categoryOneId);
        }
        if (categoryTwoId != null) {
            queryWrapper.eq(KnowledgePointArticle::getCategoryTwoId, categoryTwoId);
        }
        queryWrapper.orderByDesc(KnowledgePointArticle::getCreatedAt);
        return baseMapper.selectList(queryWrapper);
    }
    
    @Override
    public <P extends IPage<KnowledgePointArticle>> P page(P page, Wrapper<KnowledgePointArticle> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    public KnowledgePointArticle getDetailById(Integer id) {
        return baseMapper.selectById(id);
    }
    
    @Override
    public boolean add(KnowledgePointArticle article) {
        // 设置初始查看次数和使用次数为0
        article.setViewCount(0);
        article.setUseCount(0);
        return save(article);
    }
    
    @Override
    public Integer addAndReturnId(KnowledgePointArticle article) {
        // 设置初始查看次数和使用次数为0
        article.setViewCount(0);
        article.setUseCount(0);
        boolean saved = save(article);
        return saved ? article.getId() : null;
    }
    
    @Override
    public boolean update(KnowledgePointArticle article) {
        return updateById(article);
    }
    
    @Override
    public boolean removeById(Integer id) {
        return super.removeById(id);
    }
    
    @Override
    public boolean incrementViewCount(Integer id) {
        LambdaUpdateWrapper<KnowledgePointArticle> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KnowledgePointArticle::getId, id)
                    .setSql("view_count = view_count + 1");
        return update(updateWrapper);
    }
    
    @Override
    public boolean incrementUseCount(Integer id) {
        LambdaUpdateWrapper<KnowledgePointArticle> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KnowledgePointArticle::getId, id)
                    .setSql("use_count = use_count + 1");
        return update(updateWrapper);
    }
    
    @Override
    public List<KnowledgePointArticle> search(String keyword) {
        LambdaQueryWrapper<KnowledgePointArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(KnowledgePointArticle::getTitle, keyword)
                   .or()
                   .like(KnowledgePointArticle::getSummary, keyword)
                   .or()
                   .like(KnowledgePointArticle::getContent, keyword)
                   .orderByDesc(KnowledgePointArticle::getCreatedAt);
        return baseMapper.selectList(queryWrapper);
    }
}
